<?php
namespace App\Admin\Controllers;

use App\Models\Basics\SysIArea;
use App\Models\Inventory\Record;
use App\Models\Product\ProductClassify;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Illuminate\Support\Facades\Request;
class HomeController extends AdminController
{
    protected $filters = [
        'sku' => null,
        'area_id' => null,
        'season' => null,
        'classify_id' => null,
    ];

    public function index(Content $content)
    {
        $filtersField = array_keys($this->filters);
        $filters = $this->_getFilters($filtersField);
        $this->_initFilters($filters);
        $query = $this->_initQuery($filters);
        $paginator = $this->_paginator($query);
        $action = getAction($filtersField);
        $filtersBlade = [];
        $filtersBlade['sku'] = $this->filters['sku'];
        $filtersBlade['area_id'] = getOptions(SysIArea::options(),$this->filters['area_id']);
        $filtersBlade['season'] = getOptions(ProductClassify::$_season,$this->filters['season']);
        $filtersBlade['classify_id'] = getOptions(ProductClassify::options(),$this->filters['classify_id']);
        return $content
            ->title('首页')
            ->description('待办事项&到货记录')
            ->row(function (Row $row)  use($paginator,$filtersBlade,$action){
                $row->column(5, function (Column $column) {
                    $column->append(Dashboard::pendingItem()); //待办事项（首页，仪表盘）
                });
                $row->column(7, view('dashboard.record.index', compact('paginator','filtersBlade','action')));
            });
    }

    private function _getFilters($filtersField){
        $filters = [];
        foreach ($filtersField as $field){
            $filters[$field] = request($field);
        }
        return $filters;
    }

    private function _initFilters($filters){
        foreach ($filters as $field => $filter){
            $this->filters[$field] = $filter;
        }
    }

    private function _initQuery($filters){
        $query = Record::query()
            ->where('record_type',1)
            ->orderBy('id','DESC');
        foreach ($filters as $field => $filter){
            if(!$filter)continue;
            if($field == 'sku'){
                $query->whereHas('sku', function ($query) use($filter){
                    $query->where('psku_code', 'like', "%{$filter}%")
                        ->orWhere('psku_cn', 'like', "%{$filter}%");
                });
            }
            if($field == 'classify_id'){
                $query->whereHas('sku', function ($query) use($filter){
                    $query->where('classify_id', $filter);
                });
            }
            if($field == 'season'){
                $query->whereHas('sku.classify', function ($query) use($filter){
                    $query->where('season', $filter);
                });
            }
            if(in_array($field,['area_id'])){
                $query->where($field,$filter);
            }
        }
        return $query;
    }

    private function _paginator($query){
        return $query->paginate((int) (request("per_page",10)))->appends(Request::input());
    }
}
